import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class GUI extends JApplet {

	private static final long serialVersionUID = 1L;
	private JTextField balanceTextField;
	private JTextField withdrawTextField;
	
	Bank theBank = new Bank();

	public GUI() {
		getContentPane().setLayout(null);
		
		// LABELS
		JLabel lblAccountList = new JLabel("Account List");
		lblAccountList.setBounds(28, 30, 192, 14);
		getContentPane().add(lblAccountList);
		
		JLabel lblAccountInfomation = new JLabel("Account Infomation");
		lblAccountInfomation.setBounds(28, 131, 192, 14);
		getContentPane().add(lblAccountInfomation);
		
		JLabel lblBalance = new JLabel("Balance");
		lblBalance.setBounds(129, 188, 91, 14);
		getContentPane().add(lblBalance);
		
		JLabel lblWithdraw = new JLabel("Withdraw");
		lblWithdraw.setBounds(129, 258, 91, 14);
		getContentPane().add(lblWithdraw);
		
		JLabel lblUsd = new JLabel("USD");
		lblUsd.setBounds(366, 160, 46, 14);
		getContentPane().add(lblUsd);
		
		JLabel lblUsd_1 = new JLabel("USD");
		lblUsd_1.setBounds(366, 230, 46, 14);
		getContentPane().add(lblUsd_1);
		
		// BUTTONS
		JButton acc1Button = new JButton("Account 1");
		acc1Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acc1ButtonActionPerformed(e);
			}
		});
		acc1Button.setBounds(28, 55, 91, 23);
		getContentPane().add(acc1Button);
		
		JButton acc2Button = new JButton("Account 2");
		acc2Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acc2ButtonActionPerformed(e);
			}
		});
		acc2Button.setBounds(129, 55, 91, 23);
		getContentPane().add(acc2Button);
		
		JButton acc3Button = new JButton("Account 3");
		acc3Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acc3ButtonActionPerformed(e);
			}
		});
		acc3Button.setBounds(230, 55, 91, 23);
		getContentPane().add(acc3Button);
		
		JButton acc4Button = new JButton("Account 4");
		acc4Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				acc4ButtonActionPerformed(e);
			}
		});
		acc4Button.setBounds(331, 55, 91, 23);
		getContentPane().add(acc4Button);
		
		JButton displayButton = new JButton("Display");
		displayButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayButtonActionPerformed(e);
			}
		});
		displayButton.setBounds(28, 156, 91, 23);
		getContentPane().add(displayButton);
		
		// TEXT-FIELDS
		balanceTextField = new JTextField();
		balanceTextField.setBounds(129, 157, 227, 20);
		getContentPane().add(balanceTextField);
		balanceTextField.setColumns(10);
		
		withdrawTextField = new JTextField();
		withdrawTextField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				withdrawTextFieldActionPerformed(e);
			}
		});
		withdrawTextField.setBounds(129, 227, 227, 20);
		getContentPane().add(withdrawTextField);
		withdrawTextField.setColumns(10);
	}
	
	private void withdrawTextFieldActionPerformed(ActionEvent e) {
		theBank.currentAcc.balance = theBank.currentAcc.balance - Integer.parseInt(withdrawTextField.getText());;
	}
	
	private void acc1ButtonActionPerformed(ActionEvent e) {
		theBank.selectAcc1();
	}
	
	private void acc2ButtonActionPerformed(ActionEvent e) {
		theBank.selectAcc2();
	}
	
	private void acc3ButtonActionPerformed(ActionEvent e) {
		theBank.selectAcc3();
	}
	
	private void acc4ButtonActionPerformed(ActionEvent e) {
		theBank.selectAcc4();
	}
	
	private void displayButtonActionPerformed(ActionEvent e) {
		balanceTextField.setText(Integer.toString(theBank.currentAcc.getBalance()));
	}
}
